/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

package testing;
import java.util.Random;

import lime2.*;
import lights.adapters.*;
import lights.interfaces.*;

/**
 * A simple agent installing a strong once reaction which performs remote operations: when a tuple matching template
 * <code>String</code> is inserted in (the local projection of the tuplespace of) this host, it removes it and then 
 * it looks for a host on which a tuple matching template <code>String</code> exists, then outs tuple 
 * <code>"ok"</code> for that host. 
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class DistReaction implements Runnable, ReactionListener 
{
 // identification number	
 private int id = new Random().nextInt(1000);			
	
 // private data members
 private LimeTupleSpace lts = LimeTupleSpace.get("testing");	
 private ITuple strings = new Tuple().add(new Field().setType(String.class));
 private LimeTemplate template1 = new LimeTemplate(Location.HERE, Location.HERE, strings);	
 private LimeTemplate template2 = new LimeTemplate(Location.UNSPECIFIED, Location.UNSPECIFIED, strings);	

 /**
  * Run method.
  */
 public void run()
 {
  System.out.println("DistReaction " + id + ": started");     	
  Reaction x = new Reaction(Reaction.ONCE, template1, this);	
  lts.addStrongReaction(new Reaction[] {x}); 
  System.out.println("DistReaction " + id + ": dies");
 }
 
 /**
  * Reactive method.
  */
 public void reactsToOut(ReactionEvent re)
 {
  System.out.println("DistReaction " + id + ": fired");     	
  lts.inp(template1);	
  LimeTuple ltuple = lts.rdp(template2);
  if (ltuple != null)
  {	
   System.out.println("DistReaction " + id + ": found tuple on " + ltuple.getCurrent());     
   lts.out(ltuple.getCurrent(), new Tuple().add(new Field().setValue("ok")));
   System.out.println("DistReaction " + id + ": inserted tuple <ok>");  
  }
  else System.out.println("DistReaction " + id + ": nothing found");  
 }

@Override
public void reactsToTake(ReactionEvent re) {
	// TODO Auto-generated method stub
	
}

}
